﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using log4net;
using Common;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;
using SysAdmin._02_Common;
using SysAdmin._03_Service._00_Db;
using SysAdmin._04_Form._00_Common;

namespace SysAdmin._04_Form._03_Order
{
    public partial class OrderForm : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(OrderForm));

        public OrderForm()
        {
            InitializeComponent();
        }

        private void OrderForm_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            //System.Diagnostics.Trace.WriteLine(string.Format("width: {0}, height: {1}",
            //    Width, Height));
            //int width2 = Width;
            //int height2 = Height;
            //int width = Parent.Width;
            //int height = Parent.Height;
        }

        private void OrderForm_Shown(object sender, EventArgs e)
        {
            dataGridView1.Height = Height - (Padding.Top + Padding.Bottom) - panel1.Height - 10;

            {
                comboBox1.ValueMember = "Value";
                comboBox1.DisplayMember = "Text";
                comboBox1.BeginUpdate();
                comboBox1.Items.Add(new ComboBoxItemData<int>
                {
                    Value = -1,
                    Text = "全部状态"
                });
                comboBox1.Items.Add(new ComboBoxItemData<int>
                {
                    Value = (int)(EOrderState.Unpaid),
                    Text = "未付款"
                });
                comboBox1.Items.Add(new ComboBoxItemData<int>
                {
                    Value = (int)(EOrderState.Paid),
                    Text = "已付款"
                });
                comboBox1.Items.Add(new ComboBoxItemData<int>
                {
                    Value = (int)(EOrderState.RecvPayment),
                    Text = "已收款"
                });
                comboBox1.Items.Add(new ComboBoxItemData<int>
                {
                    Value = (int)(EOrderState.Pickup),
                    Text = "已提货"
                });
                comboBox1.EndUpdate();
                comboBox1.SelectedIndex = 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadOrders();
        }

        private void LoadOrders()
        {
            try
            {
                string date = null;
                if (dateTimePicker1.Checked)
                {
                    date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
                }
                string orderNo = textBox1.Text.Trim();
                string name = textBox2.Text.Trim();
                int state = (comboBox1.SelectedItem as ComboBoxItemData<int>).Value;
                var ls = OrderService.QueryAllOrders(date, orderNo, name, state);
                var ls2 = ls.Select(x => OrderBuySellInfo_2_DataGridItemData(x))
                            .OrderByDescending(x => x.Id)
                            .ToList();
                dataGridView1.DataSource = ls2;
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }


        private DataGridItemData OrderBuySellInfo_2_DataGridItemData(OrderBuySellInfo obs)
        {
            var data = new DataGridItemData();

            data.Id = obs.Order.Id;
            data.OrderNo = obs.Order.OrderNo;
            data.Name = obs.Order.Name;
            data.Buyer = obs.Buyer.Name;
            data.Amount = MyCommonTool.ToString(obs.Order.Amount);
            data.Seller = obs.Seller.Name;
            data.Time = obs.Order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
            data.State = EnumTool.GetDescription<EOrderState, int>((EOrderState)obs.Order.State);

            return data;
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.RowIndex >= 0)
                {
                    if (e.ColumnIndex == 8)
                    {
                        if (UICommonTool.ShowConfirmDialog("确定要删除订单吗？"))
                        {
                            int orderId = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                            bool b = OrderService.Delete(orderId);
                            if (b)
                            {
                                UICommonTool.ShowInfoDialog("删除成功！");
                                LoadOrders();
                            }
                            else
                            {
                                UICommonTool.ShowAlertDialog("删除失败！");
                            }
                        }
                    }
                    else if (e.ColumnIndex == 9)
                    {
                        int orderId = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                        var form = new PaymentImageForm();
                        form.OrderId = orderId;
                        form.ShowDialog();
                    }
                    else if (e.ColumnIndex == 10)
                    {
                        var form = new OrderPremiumForm();
                        form.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                UICommonTool.ShowAlertDialog(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var form = new AddOrderForm();
            form.ShowDialog();
        }

        private class DataGridItemData
        {
            public int Id { get; set; }
            public string OrderNo { get; set; }
            public string Name { get; set; }
            public string Buyer { get; set; }
            public string Amount { get; set; }
            public string Seller { get; set; }
            public string Time { get; set; }
            public string State { get; set; }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var form = new OrderPremiumForm();
            form.ShowDialog();
        }
    }
}
